<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@ page import="java.util.*" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<head>
    <title></title>
    <script src="/wms/static/js/Ctry/select.js" type="text/javascript"></script>
    <script type="text/javascript">
       $(function() {
			var warehouseCode=$("#warehouseCode").val();
			if(warehouseCode==""){
           		$("#content").html('');
           }
  		});
        function add(id){
          
           var p=$("#SelectProvince").val();
           var c=$("#SelectCity").val();
           var d=$("#SelectDistrict").val();
           var warehouseCode=$("#warehouseCode").val();
           if(warehouseCode==""){
           		alert("请选择仓库");
           		return;
           }
           
           var zoneName;
           var zoneFullName;
           var zoneType;
           var companyCode='${companyCode}';
           
           if(id=='SelectProvince'){ //省
        	  if(p=='省份') {
        		  alert('请选择省份');
        		  return;
        	  }
              zoneName=p;
              zoneFullName=p;
              zoneType='province';
           }
           else if(id=='SelectCity'){//市
        	  if(c=='城市') {
         		  alert('请选择城市');
         		  return;
         	  }
              zoneName=c;
              zoneFullName=p+c;
              zoneType='city';
           }
           else if(id=='SelectDistrict'){//县、区
        	  if(d=='区县') {
          		  alert('请选择区县');
          		  return;
          	  }
              zoneName=d;
              zoneFullName=p+c+d;
              zoneType='county';
           }
           
           $.ajax({
				type:'post',
				url:'${ctx}/v2/outScope/ajaxAddOutScope',
				data:{
					zoneName:zoneName,
					zoneFullName:zoneFullName,
					zoneType:zoneType,
					companyCode:companyCode,
					warehouseCode:warehouseCode
				},
				dataType:'json',
				success:function(data) {
				
					var msg=data.msg;
					if(msg=='success'){
					  alert("添加成功。");
					}else if(msg=='error_exist'){
					  alert("此超区已经存在。");
					}else{
					  alert("网络繁忙，请稍后重试...");
					}
					
					var settingList=data.settingList;
					var tempString='';
					$("#content").html('');
					$.each(settingList,function(i,setting){
					    tempString+="<tr>";
					    tempString+="<td style='width: 50px;'>"+(i+1)+"</td>";
					    tempString+="<td>"+setting.warehouseCode+"</td>";
					    tempString+="<td>"+setting.companyCode+"</td>";
					    tempString+="<td>"+setting.zoneName+"</td>";
					    tempString+="<td>"+setting.zoneFullName+"</td>";
					    tempString+="<td>"+new Date(setting.createTime.time).Format("yyyy-MM-dd hh:mm:ss")+"</td>";
					    tempString+="<td>"+setting.user.realname+"</td>";
					    tempString+='<td><a href="#configCustomer" onclick="configCustomer('+setting.id+');" role="button"  class="btn btn-primary" data-toggle="modal">配置不适用商户</a>&nbsp;';
					    tempString+="<button class='btn btn-danger' onclick='deleteOutScope("+setting.id+");'' >删除</button></td>";
					    tempString+="</tr>";
					});
					
					$("#content").html(tempString);
				},
				error:function() {
					alert("网络繁忙，请稍后重试...");
				}
	       });
           
        }
        
        function deleteOutScope(outScopeId){
          var companyCode='${companyCode}';
          var warehouseCode = $("#warehouseCode").val() ;
           $.ajax({
				type:'post',
				url:'${ctx}/v2/outScope/ajaxDeleteOutScope',
				data:{
					outScopeId:outScopeId,
					companyCode:companyCode,
					warehouseCode:warehouseCode
					
				},
				dataType:'json',
				success:function(data) {
				
					alert("删除成功。");
					var settingList=data.settingList;
					var tempString='';
					$("#content").html('');
					
					$.each(settingList,function(i,setting){
					    tempString+="<tr>";
					    tempString+="<td style='width: 50px;'>"+(i+1)+"</td>";
					    tempString+="<td>"+setting.warehouseCode+"</td>";
					    tempString+="<td>"+setting.companyCode+"</td>";
					    tempString+="<td>"+setting.zoneName+"</td>";
					    tempString+="<td>"+setting.zoneFullName+"</td>";
					    tempString+="<td>"+new Date(setting.createTime.time).Format("yyyy-MM-dd hh:mm:ss")+"</td>";
					    tempString+="<td>"+setting.user.realname+"</td>";
					    tempString+='<td><a href="#configCustomer" onclick="configCustomer('+setting.id+');" role="button"  class="btn btn-primary" data-toggle="modal">配置不适用商户</a>&nbsp;';
					    tempString+="<button class='btn btn-danger' onclick='deleteOutScope("+setting.id+");'' >删除</button></td>";
					    tempString+="</tr>";
					});
					$("#content").html(tempString);
				},
				error:function() {
					alert("网络繁忙，请稍后重试...");
				}
	       });
        
        }
        function select(){
    	var warehouseCode = $("#warehouseCode").val();
    	var companyCode='${companyCode}';
    	 $.ajax({
				type:'post',
				url:'${ctx}/v2/outScope/ajaxSelectOutScope',
				data:{
					companyCode:companyCode,
					warehouseCode:warehouseCode
				},
				dataType:'json',
				success:function(data) {
					var settingList=data.settingList;
					var tempString='';
					$("#content").html('');
					
					$.each(settingList,function(i,setting){
					    tempString+="<tr>";
					    tempString+="<td style='width: 50px;'>"+(i+1)+"</td>";
					    tempString+="<td>"+setting.warehouseCode+"</td>";
					    tempString+="<td>"+setting.companyCode+"</td>";
					    tempString+="<td>"+setting.zoneName+"</td>";
					    tempString+="<td>"+setting.zoneFullName+"</td>";
					    tempString+="<td>"+new Date(setting.createTime.time).Format("yyyy-MM-dd hh:mm:ss")+"</td>";
					    tempString+="<td>"+setting.user.realname+"</td>";
					    tempString+='<td><a href="#configCustomer" onclick="configCustomer('+setting.id+');" role="button"  class="btn btn-primary" data-toggle="modal">配置不适用商户</a>&nbsp;';
					    tempString+="<button class='btn btn-danger' onclick='deleteOutScope("+setting.id+");'' >删除</button></td>";
					    tempString+="</tr>";
					});
					if(warehouseCode!=''){
						$("#content").html(tempString);
					}
				},
				error:function() {
					alert("网络繁忙，请稍后重试...");
				}
	       });
    }
        var openOutScopeId = null;
        function configCustomer(outScopeId){
        	openOutScopeId = outScopeId;
        		var unAssignTempString = "";
        		var assignTempString = "";
        		$("#unAssign").html(unAssignTempString);
        		$("#assign").html(assignTempString);
        		$.ajax({
        			type : 'post',
        			url : '${ctx}/v2/outScope/ajaxOutScopeConfigCustomers',
        			data : 'outScopeId=' + outScopeId,
        			dataType : 'json',
        			success : function(data) {
        				if (data.status == "success") {
        					var allConfigCustomers = data.allConfigCustomers;
        					var assignCustomers = data.assignCustomers;
        					var flg = false;
        					for(var i = 0 ; i<allConfigCustomers.length;i++) {
        						flg = false;
        						for(var j = 0 ; j<assignCustomers.length;j++) {
        							if(allConfigCustomers[i].customerCode == assignCustomers[j].customerCode) {
        								assignCustomers[j].customerName = allConfigCustomers[i].customerName;
        								flg = true;
        							}
        						}	
        						if(!flg){
        							//未分配
        							unAssignTempString += "<option value='"+allConfigCustomers[i].customerCode+"'>"+allConfigCustomers[i].customerName+"</option>";
        						}
        					}
        					//已分配
        					for(var j = 0 ; j<assignCustomers.length;j++) {
        						assignTempString += "<option value='"+assignCustomers[j].customerCode+"'>"+assignCustomers[j].customerName+"</option>";
        					}
        					
        					
        					$("#unAssign").html(unAssignTempString);
        					$("#assign").html(assignTempString);
        					
        				} else {
        					jAlert("联系管理员！","温馨提示");
        				}
        			},

        			error : function() {
        				jAlert("联系管理员！","温馨提示");
        			}
        		});
        	
        }
        
        function saveConfigCustomer(){
       		if(!openOutScopeId) {
       			jAlert("操作异常","温馨提示");
       		}
       		var assignInfoStr = "";
       		$('#assign option').each(function(i,o){
       			assignInfoStr += $(o).val()+",";
       		});
       		$.ajax({
       			type : 'post',
       			url : '${ctx}/v2/outScope/ajaxSaveOutScopeConfigCustomers',
       			data : 'outScopeId=' + openOutScopeId+"&customerCodes="+assignInfoStr,
       			dataType : 'json',
       			success : function(data) {
       				if (data.status == "success") {
       					jAlert("保存成功！","温馨提示",function(){
       						//$("#queryForm").submit();
       					});
       				} else {
       					jAlert("联系管理员！","温馨提示");
       				}
       			},
       			error : function() {
       				jAlert("联系管理员！","温馨提示");
       			}
       		});
       	}
    </script>
</head>
<body>
<section class="content">
	<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-list"></i>
					<h3 class="box-title">添加超区</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
				<div class="form-group row">
		    <div class="col-sm-4">
<div class="input-group col-sm-12">
<span class="input-group-text"> 仓库： </span>
             <select class="form-control"  name="warehouseCode"  id="warehouseCode" >
				     <option value=""></option>  
				     <c:forEach items="${warehouseList}" var="warehouse">
				       <option value="${warehouse.groupCode}" 
				       <c:if test="${warehouseCode eq warehouse.groupCode }">selected=selected</c:if>
				       >${warehouse.groupName}</option>
				     </c:forEach>
				</select>     
					</div>
					</div>
					
 <div class="col-sm-8"><button class="btn btn-primary" onclick="select();" >查询</button></td></div>
 </div>
                   <div class="form-group row">
					 <div class="col-sm-4">
<div class="input-group col-sm-12">
<span class="input-group-text"> 省：  </span>
  <select class="form-control" id="SelectProvince" name="SelectProvince"></select>
					</div>
					</div>
					 <div class="col-sm-8"><button class="btn btn-primary"  onclick="add('SelectProvince');">添加超区</button></div>
					  </div>
                   <div class="form-group row">
					<div class="col-sm-4">
<div class="input-group col-sm-12">
<span class="input-group-text"> 市：  </span>
 <select class="form-control" id="SelectCity" name="SelectCity"></select>
					</div>
					</div>
					 <div class="col-sm-8"><button class="btn btn-primary" onclick="add('SelectCity');" >添加超区</button></div>
					  </div>
                   <div class="form-group row">
					<div class="col-sm-4">
<div class="input-group col-sm-12">
<span class="input-group-text"> 区：  </span>
  <select class="form-control" id="SelectDistrict" name="SelectDistrict"></select>
					</div>
					</div>
					 <div class="col-sm-8"><button class="btn btn-primary"  onclick="add('SelectDistrict');">添加超区</button></div>
 
					</div>
				
				
  
    
  </div>
  </div>
  </div>
  </div>
   
   	<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-list"></i>
					<h3 class="box-title">已设置的超区</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body"> 
    <table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
		
		<thead><tr>
			    <th style="width: 50px;">序号</th>
			    <th>仓库</th>
			    <th>物流公司</th>
			    <th>地区</th>
			    <th>地区全称</th>
			    <th>创建时间</th>
			    <th>创建人</th>
			    <th>操作</th>
			</tr></thead>
		<tbody id="content">
		<c:forEach items="${settingList}" var="s" varStatus="i">
			<tr>
			   <td>${i.count}</td>
			   <td>${s.warehouseCode}</td>
			   <td>${s.companyCode}</td>
			   <td>${s.zoneName}</td>
			   <td>${s.zoneFullName}</td>
			   <td>${fn:substring(s.createTime,0,19)}</td>
			   <td>${s.user.realname}</td>
			   <td>
			   	 <a href="#configCustomer" onclick="configCustomer('${s.id}');" role="button"  class="btn btn-primary" data-toggle="modal">配置不适用商户</a>
			     <button class="btn btn-danger" onclick="deleteOutScope('${s.id}');" >删除</button>
			   </td>
			</tr>
		</c:forEach>
		</tbody>
		
	</table>
    </div>
    </div>
    </div>
    </div>
            
           
   
   <script>
   	
   function toAssign() {
		$('#unAssign option:selected').each(function(i,o){
			$("#assign").append($(o));
		});
	}
		
	function toUnAssign() {
		$('#assign option:selected').each(function(i,o){
			$("#unAssign").append($(o));
		});
	}
   </script>
   <div class="modal fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" style="width:1400;" id="configCustomer">
			<div class="modal-dialog" >
			<div class="modal-content" >
			<div class="modal-header">
				<h3 id="myModalLabel">
					<font color="red">不适用</font>的商家
				</h3>
			</div>
			<div class="modal-body" style="width: 1300;height:300px; overflow: auto;">
				<table class="table table-striped table-bordered table-condensed"
					id="subWarehouseList">
					<thead>
						<tr>
							<th>未分配</th>
							<th></th>
							<th>已分配</th>
						</tr>
					</thead>
					<tr>
						<td>
							<select id="unAssign" size=12 style="width:210px;height:200px;" class="oldSelectStyle"  ondblclick="toAssign()" multiple="multiple"></select>
						</td>
						
						<td align="center" valign="middle">
							<br />
							<br />
							<br />
							<a onclick="toAssign()" role="button"  class="btn btn-primary" data-toggle="modal"> > </a>
							<br />
							<br />
							<a onclick="toUnAssign()" role="button"  class="btn btn-primary" data-toggle="modal"> < </a>
						</td>	
						
						<td>
							<select id="assign" size=12 style="width:210px;height:200px;"  class="oldSelectStyle" ondblclick="toUnAssign()"  multiple="multiple"></select>
						</td>
					</tr>
				</table>
			</div>
			<div class="modal-footer">
				<a href="#" onclick="saveConfigCustomer()" role="button"  class="btn btn-primary" data-toggle="modal">
					保存
				</a>
				<button class="btn" data-dismiss="modal" aria-hidden="true">
					关闭
				</button>
			</div>
		</div>
		</div>
	</div>
	
	 
</section>            
</body>
</html>